package scenarios.gui.actions;

import dht.cleaners.TreeCleaner;
import dht.identifier.identifier.Identifiers;
import scenarios.net.NetNodeModel;
import scenarios.net.NetNodeRow;
import scenarios.net.TestNode;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: gvennet
 * Date: 21.02.2010
 * Time: 13:10:24
 */
public class CreateNodes extends AbstractAction
{
	private ArrayList<TestNode> nodes;
	private NetNodeModel nodeModel;

	public CreateNodes(ArrayList<TestNode> nodes, NetNodeModel nodeModel)
	{
		super("Создать узлы");
		this.nodeModel = nodeModel;
		this.nodes = nodes;
	}

	public void actionPerformed(ActionEvent e)
	{
		JFileChooser fileChooser = new JFileChooser("/Users/gvennet/developing/java/IdeaProjects/dht-net/src/main/java/scenarios");
		if (fileChooser.showDialog(null, "OK") == 0)
		{
			BufferedReader fileReader = null;
			try
			{
				fileReader = new BufferedReader(new FileReader(fileChooser.getSelectedFile()));
			}
			catch (FileNotFoundException e1)
			{
				e1.printStackTrace();
			}
			//creating nodes
			String str;
			try
			{
				str = fileReader.readLine();
				while (str != null)
				{
					nodes.add(new TestNode(Identifiers.createIdentifier(str, 20), null, 0, 17, 17, 17, new TreeCleaner(Identifiers.createIdentifier(str, 20)), 20));
					nodeModel.put(new NetNodeRow(nodes.get(nodes.size() - 1).getId(), nodes.get(nodes.size() - 1).getPort(), false));
					str = fileReader.readLine();
				}
			}
			catch (IOException e1)
			{
				e1.printStackTrace();
			}
		}
	}
}
